package com.hotelvip.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author Lzh
 * @date 2021/3/3 10:30
 */
public class MonthUtils {
    /**
     * 获取连个日期之间相差的月份
     * @param startDate
     * @param endDate
     * @return
     * @throws
     */
    private static List getMonth(String startDate, String endDate) throws ParseException {
        List list = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(sdf.parse(startDate));
        c2.setTime(sdf.parse(endDate));
        int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        int month = c2.get(Calendar.MONTH) + year * 12 - c1.get(Calendar.MONTH);
        for (int i = 0; i <= month; i++) {
            c1.setTime(sdf.parse(startDate));
            c1.add(c1.MONTH, i);
            list.add(sdf.format(c1.getTime()));
        }
        return list;
    }

    public static Integer getMonthSub(String startDate, String endDate) throws ParseException {
        List month = getMonth(startDate, endDate);
        return month.size()-1;
    }


}
